package com.xuecheng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.execption.XueChengPlusException;
import com.xuecheng.mapper.TeachplanMapper;
import com.xuecheng.mapper.TeachplanMediaMapper;
import com.xuecheng.model.dto.BindTeachplanMediaDto;
import com.xuecheng.model.dto.SaveTeachplanDto;
import com.xuecheng.model.dto.TeachplanDto;
import com.xuecheng.model.po.CourseTeacher;
import com.xuecheng.model.po.Teachplan;
import com.xuecheng.model.po.TeachplanMedia;
import com.xuecheng.service.TeachplanService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 小班哥
 * @version 1.0
 * @date 2023/3/18 12:08
 * @description 课程计划查询
 */

@Service
public class TeachplanServiceImpl implements TeachplanService {

    @Autowired
    private TeachplanMapper teachplanMapper;

    @Autowired
    private TeachplanMediaMapper teachplanMediaMapper;



    /**
     * 根据id查询课程计划
     * @param id 课程ID
     * @return
     */
    @Override
    public List<TeachplanDto> findTeachplayTree(long id) {
        return teachplanMapper.selectTreeNodes(id);
    }

    /**
     * 新增/修改/保存课程计划
     * @param dto
     */
    @Override
    public void saveTeachplan(SaveTeachplanDto dto) {
        if (dto.getId() == null){
            //走这个分支是添加
            Teachplan teachplan = new Teachplan();
            BeanUtils.copyProperties(dto,teachplan);
            Integer max = teachplanMapper.SelectOrderMax(teachplan);
            if (max == null){
                max = 0;
            }
            teachplan.setOrderby(max + 1);
            teachplanMapper.insert(teachplan);
        }else {
            //修改
            Teachplan teachplan = teachplanMapper.selectById(dto.getId());
            BeanUtils.copyProperties(dto , teachplan);
            teachplanMapper.updateById(teachplan);
        }
    }

    /**
     * 删除章节信息
     * @param careerID
     */
    @Transactional
    @Override
    public void DeletePrivilegeNotHeld(long careerID) {
        Teachplan SelectTeachplan = teachplanMapper.selectById(careerID);
        //判断他是一级节点还是二级节点
        if (SelectTeachplan.getParentid() == 0){
            //判断子节点下是否还有数据，如果有不允许删除
            SelectTeachplanCount(SelectTeachplan.getId());
            teachplanMapper.deleteById(careerID);
        }else {
            //删除二级节点，以及关联的视频信息
            teachplanMapper.deleteById(careerID);
            teachplanMediaMapper.delete(new LambdaQueryWrapper<TeachplanMedia>().eq(TeachplanMedia::getTeachplanId , careerID));
        }
    }

    /**
     * 课程上移
     * @param
     */
    @Transactional
    @Override
    public void PullUP(long arenID) {
        //根据前端提交过来的数据查询到当前的数据
        Teachplan teachplan = teachplanMapper.selectById(arenID);
        //判断当前位置是否是最前的一位
        if (teachplan.getOrderby() <= 1){
            throw new XueChengPlusException("主人我已经到定了!");
        }
        teachplanMapper.PullUP(teachplan , "-");
        teachplanMapper.PullDown(teachplan , "-");

    }

    /**
     * 课程往下走一程
     * @param arenID
     */
    @Transactional
    @Override
    public void PullDown(long arenID) {
        //根据前端提交过来的数据查询到当前的数据
        Teachplan teachplan = teachplanMapper.selectById(arenID);
        //拿到最大值
        Integer max = teachplanMapper.SelectOrderMax(teachplan);
        if (teachplan.getOrderby() >= max){
            throw new XueChengPlusException("主人我已经到底了!");
        }
        teachplanMapper.PullUP(teachplan , "+");
        teachplanMapper.PullDown(teachplan , "+");
    }



    /**
     * 校验章节信息是否可以删除
     * @param ID
     * @return
     */
    private int SelectTeachplanCount(long ID) {
        LambdaQueryWrapper<Teachplan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Teachplan::getParentid , ID);
        Integer count = teachplanMapper.selectCount(queryWrapper);
        if (count > 0 ){
            throw new XueChengPlusException("课程计划信息还有子级信息,无法操作");
        }
        return count;
    }




}
